#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import json
import sys
import traceback

from flask import Flask
from flask_restful import Api

import settings
from apps.urls import register_app_urls
from init_extends import init_extends
from init_extends.exception_utils import register_except_handle
from init_extends.middleware import middleware
from init_extends.schedule_tasks.tasks import register_scheduler
from settings import Config
# !!! 需要将所有的 model 导入
from model.model import *

_app = Flask(__name__, static_url_path="/template", static_folder="./statics")
api = Api()
api.app = _app
api.init_app(_app)
# 加载各 app 路由

# 加载配置
_app.config.from_object(Config)
# 迁移指令
manager = init_extends.db_extends(_app)


def init_extends(_app):
    register_except_handle(_app)
    register_scheduler(_app)
    middleware(_app)
    register_app_urls()

    return _app


app = init_extends(_app)


if __name__ == '__main__':
    if len(sys.argv) > 1:
        manager.run()
    else:
        app.run(host=settings.SERVER_HOST, port=settings.SERVER_PORT, debug=settings.SERVER_DEBUG)
        # app.run(host="0.0.0.0", port=9090, debug=True)
